import React,{useRef} from 'react';
import {PageContainer } from '@ant-design/pro-layout';
import {message } from 'antd';
import ProForm,{ProFormDigit, ProFormText} from '@ant-design/pro-form';
import {saveManufacturer,manufacturerData} from "../service";

export default (): React.ReactNode => {
  const actionRef = useRef();

  React.useEffect(()=>{
    getData();
    return function cleanup() {};
  },[]);

  async function  getData(){
    var val = await manufacturerData();
    if(val.errorCode==='200'){
      console.log(actionRef);
      actionRef.current?.setFieldsValue(val.data);
    }else{
      message.error("获取数据失败");
    }
  }


  return (
    <PageContainer>
      <ProForm
      formRef={actionRef}
      onFinish={async (values) => {
          var val = await saveManufacturer(values);
          if(val.errorCode==='200'){
            message.success('提交成功');
            return true;
          }else{
            message.error(val.errorMessage);
            return false;
          }
      }}>
        <ProFormText width="md" name="name" label="厂家名称" placeholder="请输入厂家名称" />
        <ProFormText width="md" name="address" label="厂家地址" placeholder="请输入厂家地址" />
        <ProFormText width="md" name="phone" label="厂家电话" placeholder="请输入厂家电话" />
        <ProFormText width="md" name="mail" label="厂家邮箱" placeholder="请输入厂家邮箱" />
        <ProFormDigit width="md" name="receiveDays" label="自动收货时长(天)" placeholder="请输入时长（单位天）" />
        <ProFormText width="md" name="adminMail" label="管理员邮箱" placeholder="请输入管理员邮箱" />
      </ProForm>
    </PageContainer>
  );
};
